﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Sys_attachedfiles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView(true);
        }
    }
    //附件图片链接处理
    static public string strImage(string attach)
    {
        return (attach != string.Empty) ? "../Images/attachment-green.png" : "../Images/un-attachment.png";
    }
    //选择所有
    protected void CheckAll(object sender, EventArgs e)
    {
        CheckBox cbx = (CheckBox)sender;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox ch = (CheckBox)gvr.FindControl("ItemBox");
            ch.Checked = cbx.Checked;
        }
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        BindGridView(false);
    }
    protected void ddlAttachType_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridView(true);
    }
    /// <summary>
    /// 绑定gridview
    /// </summary>
    /// <param name="GetCount">是否获取总数</param>
    protected void BindGridView(bool GetCount)
    {
        string field = "id,filename,filepath,createdate,filetype,status";
        string where = " 1=1 ";
        if (this.ddlAttachType.SelectedValue != "0")
            where += " and filetype='"+this.ddlAttachType.SelectedValue+"' ";
        PM.Code.Web.controlbind.BindGridViewPublicProc(GridView1, AspNetPager1, "Sys_Attached", "id", field, "id asc", where, GetCount);
    }
    //结束编辑
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindGridView(false);
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindGridView(false);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //实例化附件类
        PM.PMSys.attached Att = new PM.PMSys.attached();
        //获取行主键值
        int id = Convert.ToInt16(GridView1.DataKeys[e.RowIndex].Value);
        //获取修改值
        string filename = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
        if (filename == string.Empty)
        {
            PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "文件名称不能为空！");
            return;
        }
        Att.Update_AttachedName(filename, id);
        PM.Code.Web.alertmessage.ShowMsg(this.UpdatePanel1, "更新成功！");
        this.GridView1.EditIndex = -1;
        BindGridView(false);
    }
    //删除所有（事务）
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        Hashtable sqlstrs = new Hashtable();
        int keyid = -1;
        int i = 1;
        foreach (GridViewRow gvr in GridView1.Rows)
        {
            CheckBox check = (CheckBox)gvr.FindControl("ItemBox");
            if (check.Checked)
            {
                keyid = Int32.Parse(GridView1.DataKeys[gvr.DataItemIndex].Value.ToString().Trim());
                string param = "@attachid" + i.ToString();
                string sqlstr = "delete from Sys_Attached where id=" + param;
                SqlParameter[] AttachParms = new SqlParameter[1];
                AttachParms[0] = new SqlParameter(param, keyid);
                sqlstrs.Add(sqlstr, AttachParms);
                i++;
            }
        }
        if (i > 1)
        {
            PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstrs);
            BindGridView(true);
        }
    }
}
